package com.listeningframework.boot.codegen.lang;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.text.MessageFormat;
import java.util.StringTokenizer;
import java.util.UUID;

/**
 * 
 * @description 字符串工具类
 * 
 * @author Listening
 * @version $Id: CommonStringUtils.java, v 0.1 2014年11月1日 下午6:53:21 Listening
 *          Exp $
 */
public class CodeGenStringUtils {

    private CodeGenStringUtils() {

    }

    /**
     * Check value is blank
     * 
     * @param value
     * @return
     */
    public static final boolean isBlank(String value) {
        return StringUtils.isBlank(value);
    }

    /**
     * Is Not Blank
     * 
     * @param value
     * @return
     */
    public static final boolean isNotBlank(String value) {
        return StringUtils.isNotBlank(value);
    }

    /**
     * Trim Blank Characters Or Wraps
     */
    public static final String trimWrap(String value) {
        String tempValue = value == null ? "" : value.trim();
        return tempValue.replaceAll("\\s+", "");
    }

    /**
     * IS Any Blank
     * 
     * @param value
     * @return
     */
    public static final boolean isAnyBlank(String... value) {
        return StringUtils.isAnyBlank(value);
    }

    /**
     * IS All Blank
     * 
     * @param values
     * @return
     */
    public static final boolean isAllBlank(String... values) {
        boolean result = true;
        for (String value : values) {
            if (isNotBlank(value)) {
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * Is Contains The Chars In the value
     * 
     * @param value
     * @param chars
     * @return
     */
    public static final boolean contains(String value, String chars) {
        return StringUtils.contains(value, chars);
    }

    /**
     * check string is numeric
     * 
     * @param value
     * @return
     */
    public static final boolean isNumeric(String value) {
        return StringUtils.isNumeric(value);
    }

    /**
     * To Upper Case
     * 
     * @param value
     * @return
     */
    public static final String upperCase(String value) {
        return StringUtils.upperCase(value);
    }

    /**
     * To Upper Case
     * 
     * @param value
     * @return
     */
    public static final String lowerCase(String value) {
        return StringUtils.lowerCase(value);
    }

    /**
     * The First Character to Lower case
     * 
     * @param value
     * @return
     */
    public static final String uncapitalize(String value) {
        return StringUtils.uncapitalize(value);
    }

    /**
     * The First Character to Upper case
     * 
     * @param value
     * @return
     */
    public static final String capitalize(String value) {
        return StringUtils.capitalize(value);
    }

    /**
     * Equals
     *
     * @return
     */
    public static final boolean equals(String one, String two) {
        return StringUtils.equals(one, two);
    }

    /**
     * Equals
     *
     * @return
     */
    public static final boolean equalsIgnoreCase(String one, String two) {
        return StringUtils.equalsIgnoreCase(one, two);
    }

    /**
     * 格式化字符串
     * 
     * @param value
     * @param params
     * @return String
     */
    public static final String format(String value, Object... params) {
        return MessageFormat.format(value, params);
    }

    /**
     * Long 2 Integer
     * 
     * @param value
     * @return
     */
    public static final Integer long2int(Long value) {
        return value == null ? 0 : Integer.parseInt(String.valueOf(value));
    }

    /**
     * Get Simple Json Content
     */
    public static final String simpleJSON(String success, String message) {
        return "{'success':'" + success + "', 'message':'" + message + "'}";
    }

    /**
     * Get File Suffix Name
     */
    public static final String getFileSuffix(String oriName) {
        if (StringUtils.isBlank(oriName) || oriName.indexOf(".") == -1) {
            return null;
        }
        return oriName.substring(oriName.lastIndexOf("."));
    }

    /**
     * Is Not Emoji Character
     */
    public static boolean isNotEmojiCharacter(char codePoint) {
        if ((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)) {
            return true;
        }
        if ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) {
            return true;
        }
        if ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) {
            return true;
        }
        return ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     */
    public static String filterEmoji(String source) {
        if (StringUtils.isBlank(source)) {
            return "";
        }
        int len = source.length();
        StringBuilder buf = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isNotEmojiCharacter(codePoint)) {
                buf.append(codePoint);
            }
        }
        return buf.toString().trim();
    }

    /**
     * Get UUID
     */
    public static final String getUUID() {
        return UUID.randomUUID().toString().toUpperCase().replaceAll("[-]", "");
    }

    /**
     * <p>
     * Compares two CharSequences, returning {@code true} if they represent
     * equal sequences of characters, ignoring case.
     * </p>
     *
     * <p>
     * {@code null}s are handled without exceptions. Two {@code null} references
     * are considered not equal. Comparison is case insensitive.
     * </p>
     *
     * <pre>
     * StringUtils.equalIgnoreCaseWithAny(null, null) = false
     * StringUtils.equalIgnoreCaseWithAny(null, "abc") = false
     * StringUtils.equalIgnoreCaseWithAny("abc", null, "abc") = true
     * StringUtils.equalIgnoreCaseWithAny("abc", "abc", "bcd") = true
     * StringUtils.equalIgnoreCaseWithAny("abc", "ABC", "bcd") = true
     * </pre>
     *
     * @param value
     *            the first CharSequence, can not be null
     * @param params
     *            the second CharSequence, can not be null
     */
    public static final boolean equalsIgnoreCaseWithAny(String value, String... params) {
        if (StringUtils.isBlank(value) || params == null) {
            return false;
        }
        boolean result = false;
        for (String paramVal : params) {
            if (StringUtils.isBlank(paramVal)) {
                continue;
            }
            if (StringUtils.equalsIgnoreCase(paramVal, value)) {
                result = true;
                break;
            }
        }
        return result;
    }

    /**
     * 生成Get方法名称
     * 
     * @param fieldName
     * @return String
     */
    public static final String generateGetMethodName(String fieldName) {
        return "get" + capitalize(fieldName);
    }

    /**
     * 生成Set方法名称
     * 
     * @param fieldName
     * @return String
     */
    public static final String generateSetMethodName(String fieldName) {
        return "set" + capitalize(fieldName);
    }

    /**
     * 包名转路径
     * 
     * @param packageName
     * @return String
     */
    public static final String package2Path(String packageName) {
        return packageName.replace(".", File.separator);
    }

    /**
     * 包名转路径
     * 
     * @param path
     * @return String
     */
    public static final String separator(String path) {
        return path.replace("/", File.separator);
    }

    public static final String treatColumnType(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return value.replaceAll("\\(.*\\)", "").trim().toUpperCase();
    }

    /**
     * Column Field To Java
     * 
     * @param fieldName
     * @return String
     */
    public static String columnFieldToJava(String fieldName) {
        if (StringUtils.isBlank(fieldName)) {
            return null;
        }
        StringTokenizer tokenizer = new StringTokenizer(fieldName, "_");
        int index = 0;
        StringBuilder result = new StringBuilder();
        while (tokenizer.hasMoreTokens()) {
            String value = tokenizer.nextToken();
            if (index == 0) {
                result.append(value.toLowerCase());
            } else {
                result.append(StringUtils.capitalize(value.toLowerCase()));
            }
            index++;
        }
        return result.toString();
    }

    public static String rpad(String value, int totalLength, String padChar) {
        String tempValue = StringUtils.isBlank(value) ? "" : value.trim();
        Integer padLngth = totalLength - tempValue.length();
        if (padLngth <= 0) {
            return tempValue;
        }
        StringBuilder builder = new StringBuilder();
        builder.append(tempValue);
        for (int i = 0; i < padLngth; i++) {
            builder.append(padChar);
        }
        return builder.toString();
    }
}
